package com.boguan.system.service.impl;

import com.boguan.common.core.domain.entity.SysUser;
import com.boguan.common.utils.SecurityUtils;
import com.boguan.common.utils.StringUtils;
import com.boguan.system.domain.Protocol;
import com.boguan.system.mapper.ProtocolMapper;
import com.boguan.system.service.ISysProtocolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


/**
 * 协议内容处理
 */
@Service
public class SysProtocolServiceImpl implements ISysProtocolService {

    @Autowired
    private ProtocolMapper protocolMapper;

    @Override
    public int saveProtocol(Protocol protocol) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Protocol protocol1 = protocolMapper.selectProtocolByType(protocol.getType());
        int cont = 0;
        if (StringUtils.isNull(protocol1)) {
            protocol.setUpdateUser(user.getUserId());
            protocol.setUpdatetime(new Date());
            protocol.setCreateUser(user.getUserId());
            cont = protocolMapper.insertSelective(protocol);
        } else {
            protocol1.setContent(protocol.getContent());
            protocol1.setUpdateUser(user.getUserId());
            protocol1.setUpdatetime(new Date());
            cont = protocolMapper.updateByPrimaryKeySelective(protocol1);
        }
        return cont;
    }

    @Override
    public Protocol getProtocol(Integer type) {
        Protocol protocol = protocolMapper.selectProtocolByType(type);
        if (StringUtils.isNull(protocol)) {
            protocol = new Protocol();
        }
        return protocol;
    }
}
